package com.mgs.tagger.controller
{
	import com.mgs.tagger.appdata.FileItemPictureResolver;
	import com.mgs.tagger.appdata.FileItemResolver;
	import com.mgs.tagger.appdata.ItemSizeConstant;
	import com.mgs.tagger.appdata.PersistenceManager;
	import com.mgs.tagger.appdata.dto.DirectoryDTO;
	import com.mgs.tagger.appdata.dto.FileItemDTO;
	import com.mgs.tagger.appdata.dto.TagDTO;
	import com.mgs.tagger.fsdata.DirectoryScanner;
	import com.mgs.tagger.fsdata.FileTypeExtensions;
	import com.mgs.tagger.fsdata.FsDataUtil;
	import com.mgs.tagger.log.Level;
	import com.mgs.tagger.log.Logger;
	import com.mgs.tagger.model.FileItemUI;
	import com.mgs.tagger.model.Model;
	
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.system.fscommand;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayList;
	
	public class WorkspaceController extends EventDispatcher
	{
		private static var _instance:WorkspaceController;
		
		public static function get instance():WorkspaceController{
			if(_instance == null){
				_instance = new WorkspaceController();
			}
			return _instance;
		}
		
		function WorkspaceController()
		{
		}
		public function load():void{
			Model.instance.workspace = PersistenceManager.instance.load();
			// update model directories.
			Model.instance.directories = new ArrayList();
			for each(var d:DirectoryDTO in Model.instance.workspace.directories){
				Model.instance.directories.addItem(d);
			}
			updateElements();
			TagController.instance.load(Model.instance.workspace);
		}
		public function save():void{
			PersistenceManager.instance.save(Model.instance.workspace);
		}
		public function addDirectory(d:DirectoryDTO):void{
			Model.instance.directories.addItem(d);
			Model.instance.workspace.directories.push(d);
			updateElements();
		}
		public function remDirectory(d:DirectoryDTO):void{
			Model.instance.directories.removeItem(d);
			Model.instance.workspace.directories.splice(Model.instance.workspace.directories.indexOf(d), 1);
			updateElements();
		}
		
		private function updateElements():void{
			var resolver:FileItemResolver = new FileItemResolver(Model.instance.workspace);
			
			Model.instance.allFileItems = new ArrayList();
			Logger.instance.log(Level.INFO, "scan workspace");
			for each(var d:DirectoryDTO in Model.instance.workspace.directories){
				Logger.instance.log(Level.INFO, "scan directory " + d.filename);
				var f:File = new File(d.filename);
				if(f.exists){
					for each(var subfile:File in f.getDirectoryListing()){
						Logger.instance.log(Level.INFO, "scan item " + subfile.name);
						try{
							var item:FileItemUI = new FileItemUI();
							item.name = subfile.name;
							item.dto = resolver.resolveFileItem(subfile.name);
							item.dto.filename = item.name;
							item.tags = new ArrayList();
							item.file = subfile;
							
							var pictureResolver:FileItemPictureResolver = new FileItemPictureResolver(item.dto);
							pictureResolver.resolve(subfile);
							item.indexPicture = pictureResolver.indexPicture;
							item.pictures = pictureResolver.pictures;
							item.numPictures = pictureResolver.pictures.length;
							item.videos = pictureResolver.videos;
							item.numVideos = pictureResolver.videos.length;
							
							if(item.numPictures > 0){
								Logger.instance.log(Level.INFO, "load picture file " + item.pictures[item.indexPicture].name);
								item.icon = FsDataUtil.loadDataFromFile(item.pictures[item.indexPicture]);
							}
							
							for each(var t:TagDTO in item.dto.tags){
								item.tags.addItem(t);
							}
							
							Model.instance.allFileItems.addItem(item);
						}catch(e:Error){
							Logger.instance.log(Level.SEVERE, "error scanning " + subfile.name, e);
						}
					}
				}else{
					// TODO store unresolved folder
					Logger.instance.log(Level.WARNING, "directory not found " + d.filename);
				}
			}
			TagController.instance.updateItems();
		}
		
	}
}